import { PublishBase } from "./PublishBase";
import { join } from "path";
import * as rimraf from 'rimraf';
import { cpDir, createManifest } from "../utils/FileUtil";
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
import { paserHtml, mergeJs, downloadZxSdk } from "./PaserHtmlUtil";

export class WxMiniGamePublish1 extends PublishBase {
    get projDir() { return "wxgame" };
    createProject() {
        let tplPath = this.mTempProjPath
        let toPath = this.mProjPath
        if (existsSync(toPath)) {
            this.doPack();
            return;
        }
        this.log('创建项目目录:' + toPath);
        rimraf(toPath, (err2) => {
            if (err2) {
                console.warn(`清理目录${toPath}出错`);
                return;
            }

            cpDir(tplPath, toPath, () => {
                let projCfgPath = join(toPath, 'project.config.json');
                let fstr = readFileSync(projCfgPath).toString();
                fstr = fstr.replace('${APPID}', this._publishVo.content.appId);
                fstr = fstr.replace('${PORJECT_NAME}', encodeURI(this._publishVo.name));
                writeFileSync(projCfgPath, fstr);

                this.log('项目生成完毕 下一步可以开始打包游戏了');
                this.doPack();
            });
        });
    }


    doPack() {
        // 解析html文件
        let htmlObj = paserHtml(this._publishVo.fromPath);

        this.log('开始修改config.js');
        // 写入config
        let crtProjPath = this.mProjPath
        writeFileSync(join(crtProjPath, this.getConfigName()), "var config = " + this.config + ";module.exports = config;");

        this.log('开始合并js代码');
        // 合并js列表
        mergeJs(htmlObj.js, this._publishVo.fromPath, crtProjPath, 'code.js', () => {
            this.log('开始hash素材');
            downloadZxSdk(htmlObj.html, crtProjPath, () => {
                this.log('zxsdk下载完毕');
                if (!this._checkRes) {
                    this.log('素材不需要重新生成');
                    this.packOver();
                    return;
                }
                let resPath = join(this._publishVo.toPath, this._version);
                if (existsSync(resPath)) {
                    rimraf(resPath, (delErr) => {
                        if (delErr) {
                            console.warn('清理目录出错', resPath);
                            return;
                        }
                        this.cpRes(resPath);
                    });
                } else {
                    this.cpRes(resPath);
                }

            });

        });
    }

    cpRes(resPath: string) {
        if (!existsSync(resPath)) {
            mkdirSync(resPath);
        }
        // 拷贝素材
        cpDir(join(this._publishVo.fromPath, 'res'), join(resPath, 'res'), () => {
            createManifest(resPath, (info) => {
                console.log('info;', info);
                if (info === null) {
                    console.log(`素材manifest: res_${this._version}.json 生成出错`);
                    return;
                }
                writeFileSync(join(resPath, `res_${this._version}.json`), JSON.stringify(info));
                this.log('素材拷贝完毕');
                this.packOver();
            });

        }, true, { dir: ['\W+\.svn'] });
    }

    getConfigName() {
        return 'config.js';
    }
}